#include"MergeSort.h"

void Swap(int* x1, int* x2)
{
	int mid = *x1;
	*x1 = *x2;
	*x2 = mid;
}

void MergeSort(int* arr, int left,int right,int*temp)
{

     int mid = (left + right) / 2;
	int begin1 = left, end1 = mid,begin2=mid+1,end2=right;
	int idex = begin1;
	
	if (left >= right) return;

	MergeSort(arr, left, mid,temp);
	MergeSort(arr, mid + 1, right,temp);

	while (begin1 <= end1 && begin2 <= end2)
	{
		if (arr[begin1] < arr[begin2])
		{
			temp[idex++] = arr[begin1++];
		}
		else
		{
			temp[idex++] = arr[begin2++];
		}
	}

	while (begin1 <= end1)
	{
		temp[idex++] = arr[begin1++];
	}
	while (begin2 <= end2)
	{
		temp[idex++] = arr[begin2++];
	}

	for (int i = left; i <= right; i++)
	{
		arr[i] = temp[i];
	}
}

void _MergeSort(int* arr, int r)
{
	int* temp = (int*)malloc(sizeof(int) * (r + 1));

	MergeSort(arr,0, r, temp);
	free(temp);
	temp = NULL;
}